
var app = getApp()
// 语言
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeTab:0,
    p:1,
    list:[],
  },
  changeTab(e){
    this.setData({
      p:1,
      list:[],
      activeTab:e.currentTarget.dataset.index
    })
    this.mDetail(this.data.id)
  },
  // 查看冻结余额
  showTips(){
    // wx.showModal({
    //   showCancel:false,
    //   title:"冻结余额",
    //   content:"冻结余额将在拼团订单所有 用户确认收货后转为可用余额 ",
    //   confirmText:"我知道了"
    // })
    wx.navigateTo({
      url: '/pages/freeze/freeze?id='+this.data.id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    if(e) this.setData(e)
   
  },
  onShow(){
       // 登录
     let login  = wx.getStorageSync('isPhone');
     let customer_id = wx.getStorageSync('customer_id');
     if(!login  && ( !customer_id || (customer_id && (customer_id.head_url==""|| customer_id.phone=="" || !customer_id.phone || !customer_id.head_url)))){
       wx.navigateTo({
         url: '/pages/login/login?phone=true',
       })
     }

     // 商家
     this.merchant();
  },
  // 商家数据
  merchant(b){
    b = b||this.data.b;
    let customer_id = wx.getStorageSync('customer_id')
    var that=this;
    wx.request({
      url: app.globalData.urls + (b ? '/wallet/wallet' : '/wallet/customer-wallet'),
      data: {
        ...(b? {bdmin_user_id:util.getBdmin_user_id()} : {
          customer_id:customer_id.customer_id
        }),
      },
      method:'POST',
      header: app.getRequestHeader(),
      success: (res) => {
        if (res.data.code == 200) {
          let data=res.data.data;
          data.totals = (Number(data.available_money) + Number(data.frozen_money)).toFixed(2)
          console.log("data",data)
          that.setData(data);
          that.mDetail(data.id);
        }
      }
    })
  },
  // 商家明细
  mDetail(id){
    var that=this;
    wx.request({
      url: app.globalData.urls + '/wallet/detail-list',
      data: {
        id:id,
        p:this.data.p,
        type:this.data.activeTab,
        status:2
      },
      method:'POST',
      header: app.getRequestHeader(),
      success: (res) => {
        wx.hideLoading();
        if (res.data.code == 200) {
          let data=res.data.data;
          that.setData({
            totalPage:data.totalPage,
            list:that.data.list.concat(data.list)
          })
        }
      }
    })
  },
  // 去提现
  gotoWithdraw(){
    let money = this.data.available_money;
    if(money>0){
      wx.navigateTo({
        url: '/pages/withdraw/withdraw?total='+money+"&id="+this.data.id,
      })
    }else{
      wx.showToast({
        title: '当前没有可提现金额',
        icon:'none'
      })
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let p=this.data.p
    if(p<this.data.totalPage){
      this.setData({
        p:p+1
      })
      this.mDetail(this.data.id)
    }
  },
})